package cc.zhaoac.faith.core.demo.controller;


import cc.zhaoac.faith.core.boot.annotation.Trim;
import cc.zhaoac.faith.core.boot.ctrl.FaithController;
import cc.zhaoac.faith.core.demo.model.User;
import cc.zhaoac.faith.core.demo.service.UserService;
import cc.zhaoac.faith.core.tool.api.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户表(User)表控制层
 *
 * @author zhaoanchi
 * @since 2023-09-18 16:56:53
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController extends FaithController {

    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping("page")
    public R selectAll(Page<User> page, User user) {
        return R.data(this.userService.page(page, new QueryWrapper<>(user)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping
    public R selectOne(Serializable id) {
        return R.data(this.userService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody User user) {
        return R.data(this.userService.save(user));
    }

    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody User user) {
        return R.data(this.userService.updateById(user));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.data(this.userService.removeByIds(idList));
    }


    @GetMapping("/alwaysUpdate")
    public void alwaysUpdate() {
        LambdaUpdateWrapper<User> wrapper = Wrappers.lambdaUpdate(User.class)
                .set(User::getUsername, "123")
                .set(User::getPassword, "321")
                .eq(User::getId, 3);
        userService.alwaysUpdate(wrapper);
    }

    @GetMapping("/alwaysSelectById")
    public R<User> alwaysSelectById() {
        return data(userService.alwaysGetById(3));
    }
    @GetMapping("/alwaysListBatchIds")
    public void alwaysListBatchIds() {

        userService.alwaysListBatchIds(Collections.singletonList(3));
    }

    @GetMapping("/dynamicLog")
    public void dynamicLog() {
        log.trace("tarce");
        log.debug("debug");
        log.info("info");
        log.warn("ware");
        log.error("error");
    }

    @GetMapping("/trim")
    public R<String> trim(@Trim String str) {
        log.info("]" + str + "[");
        return data(str);
    }

    @PostMapping("/trim")
    public R<String> trim(@RequestBody User user) {
        log.info("]" + user.getName() + "[");
        return data(user.getName());
    }

    @GetMapping("/test/date")
    public void trim(@DateTimeFormat(pattern = "yyyy/MM/dd") Date date) {
        log.info("]" + date.toString() + "[");
    }
}

